package com.jxj.RestTestController;

import jdk.nashorn.internal.ir.debug.ClassHistogramElement;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;

@Controller
public class IndexController {

    @RequestMapping("/main")
    public String index(){
        return "index";
    }

    @RequestMapping("/inStaticFile")
    @ResponseBody
    public String inStaticFile(HttpServletRequest request, HttpServletResponse response,String pathname,String date){
        byte[] sourceByte = date.getBytes();
        try {
            String path = ResourceUtils.getURL("classpath:").getPath()+"static/img/";
            File file = new File(path + pathname+".txt");//文件路径（路径+文件名）
            if (!file.exists()) {   //文件不存在则创建文件，先创建目录
                System.out.println(" aa = "+file.getParent());
                File dir = new File(file.getParent());
                boolean mkdirs = dir.mkdirs();
                boolean newFile = file.createNewFile();
            }
            FileOutputStream outStream = new FileOutputStream(file); //文件输出流将数据写入文件
            outStream.write(sourceByte);
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "成功";
    }
}
